<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="${pageContext.request.contextPath }/assets/css/layui.css"  media="all">
	<link rel="stylesheet" href="${pageContext.request.contextPath }/assets/css/toastr.min.css"  media="all">
</head>
<body>
<div class="demoTable" style="padding:10px 0 0 15px">
    <div class="layui-inline">
        <input class="layui-input" name="keyword" id="keyword" placeholder="请输入设备名称" autocomplete="off">
    </div>
    <button class="layui-btn layui-btn-blue" style="margin: 0 0 0 10px" data-type="reload"><i class="layui-icon layui-icon-search">搜索</i></button>
</div>
<table class="layui-hide" id="test" lay-filter="test"></table>
<script type="text/html" id="toolbarDemo">
	<div class="layui-btn-container">
		<button class="layui-btn layui-btn-blue" lay-event="add"><i class="layui-icon layui-icon-add-1">新增</i></button>
		<button class="layui-btn layui-btn-blue" lay-event="delete"><i class="layui-icon layui-icon-delete">批量删除</i></button>
	</div>
</script>
<script type="text/html" id="rowToolbar">
	<%--<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>--%>
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="${pageContext.request.contextPath }/assets/layui.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath }/assets/layui.all.js"></script>
<script src="${pageContext.request.contextPath }/assets/js/jquery.min.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath }/assets/js/toastr.min.js" charset="utf-8"></script>
<script type="text/javascript">
layui.use(['table','layer','upload','form'], function(){
	var table = layui.table;
	var layer = layui.layer;
    var laydate = layui.laydate;
    var form = layui.form;

    var $ = layui.jquery, active = {
        reload:function () {
            var keyword01 = $("#keyword").val();
            console.log(keyword)
            table.reload('contenttable',{
                method:'get',
                where:{keyword01:keyword01},
				page: {
					curr: 1
				}
            });
        }
    }
    $('.layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    table.render({
        elem: '#test'
        ,url:'${pageContext.request.contextPath }/maintResume/queryDeviceMaintenanceRecords.do'
        ,toolbar: '#toolbarDemo'
        ,title: '用户数据表'
        ,id :'contenttable'
        ,limits:[10,20,30]
        ,cols: [[
            {type: 'checkbox', fixed: 'left'}
            ,{field:'', title:'序号', sort: true, type:'numbers', width: 60}
            ,{field:'deviceName', title:'设备名称'}
            ,{field:'deviceNo', title:'设备编号'}
            ,{field:'repairTime', title:'报修时间'}
            ,{field:'repairMan', title:'报修人'}
            ,{field:'errorDescribe', title:'故障现象描述'}
            ,{field:'errorAnalysis', title:'故障原因分析'}
            ,{field:'repairHandle', title:'处理措施'}
            ,{field:'repairResult', title:'维修结果'}
            ,{field:'op1', title:'更换备件详细', templet:function (rowData){
                return "<a href='javascript:void(0)' style='color:#4D61B3' onclick='deviceMaintenanceRecordDetailEvent(\""+rowData.id+"\")'>"+'查看详细'+"</a>";
            }}
            ,{fixed: 'right', title:'操作', toolbar: '#rowToolbar', align: 'center', width: 110}
        ]]
        ,page: true
    });
  
	//头工具栏事件
	table.on('toolbar(test)', function(obj){
		var checkStatus = table.checkStatus(obj.config.id);
	    switch(obj.event){
            case 'add':
                layer.open({
                    type: 1 					//Page层类型
                    ,area: ['760px', '450px'] 	//宽  高
                    ,title: '新增'
                    ,shade: 0.6 				//遮罩透明度
                    ,maxmin: true 				//允许全屏最小化
                    ,anim: 1 					//0-6的动画形式，-1不开启
                    ,content: '<form class="layui-form" id="addformID">'+
                        '<div style="border: 1px solid #E6E6E6; margin: 15px 15px; width: 715px">'+
                        '<table>'+
                        '<tr>'+
                        	'<td><label class="layui-form-label" style="margin-left:-10px; margin-top:9px">设备编号</label></td>&nbsp;'+
                        	'<td>'+
	                        	'<div class="layui-input-block" style="width: 230px; margin-top: 10px; margin-left:-3px">'+
									'<select class="layui-select" id="deviceNo" name="deviceNo" lay-filter="displayInfo" lay-verify="deviceNo">'+
										'<option value="">请选择</option>'+
									'</select>'+
								'</div>'+
                        	'</td>'+
		                    '<td><label class="layui-form-label" style="margin-left:-10px; margin-top:9px">设备名称</label></td>'+
		                    '<td><input class="layui-input" type="text" id="deviceName" name="deviceName" style="width:230px; margin-top:10px; margin-left:27px; display: inline;"></td>'+
                        '</tr>'+
                        '</table>'+
                        '&emsp;&emsp;报修时间&emsp;<input class="layui-input" type="text" id="repairTime" name="repairTime" style="width:230px; margin-top:15px; display: inline;">'+
                        '&emsp;&emsp;报修人&emsp;&emsp;&emsp;&emsp;<input class="layui-input" type="text" id="repairMan" name="repairMan" style="width:230px; margin-top:15px; margin-bottom: 15px; display: inline;"><br>'+
                        '&emsp;&emsp;故障现象描述&emsp;'+
                        '<textarea class="layui-textarea" id="errorDescribe" name="errorDescribe" style="width: 585px; height: 100px; margin-top: 10px; margin-left: 100px; margin-bottom: 15px"></textarea>'+
                        '&emsp;&emsp;故障原因分析&emsp;'+
                        '<textarea class="layui-textarea" id="errorAnalysis" name="errorAnalysis" style="width: 585px; height: 100px; margin-top: 10px; margin-left: 100px; margin-bottom: 15px"></textarea>'+
                        '&emsp;&emsp;处理措施&emsp;'+
                        '<textarea class="layui-textarea" id="repairHandle" name="repairHandle" style="width: 585px; height: 100px; margin-top: 10px; margin-left: 100px; margin-bottom: 15px"></textarea>'+
                        '<div style="width: 90px; height: 30px; margin-left: 55px; margin-top: 15px; float: left;">更换备件名称</div>'+
                        '<div style="width: 60px; height: 30px; margin-left: 125px; margin-top: 15px; float: left;">型号规格</div>'+
                        '<div style="width: 60px; height: 30px; margin-left: 120px; margin-top: 15px; float: left;">数量</div>'+
                        '<div style="width: 60px; height: 30px; margin-left: 95px; margin-top: 15px; float: left;">单位</div><br>'+
                        '<input class="layui-input" id="changeDeviceName" name="changeDeviceName" type="text" style="width:128px; margin-left: 27px; margin-top:5px; display: inline;">'+
                        '<input class="layui-input" id="deviceVersion" name="deviceVersion" type="text" style="width:128px; margin-left: 60px; margin-top:5px; display: inline;">'+
                        '<input class="layui-input" id="deviceCount" name="deviceCount" type="text" style="width:128px; margin-left: 55px; margin-top:5px; display: inline;">'+
                        '<input class="layui-input" id="deviceUnit" name="deviceUnit" type="text" style="width:128px; margin-left: 27px; margin-top:5px; display: inline;"><br>'+
                        '<input class="layui-input" id="changeDeviceName" name="changeDeviceName" type="text" style="width:128px; margin-left: 27px; margin-top:5px; display: inline;">'+
                        '<input class="layui-input" id="deviceVersion" name="deviceVersion" type="text" style="width:128px; margin-left: 60px; margin-top:5px; display: inline;">'+
                        '<input class="layui-input" id="deviceCount" name="deviceCount" type="text" style="width:128px; margin-left: 55px; margin-top:5px; display: inline;">'+
                        '<input class="layui-input" id="deviceUnit" name="deviceUnit" type="text" style="width:128px; margin-left: 27px; margin-top:5px; display: inline;"><br>'+
                        '<input class="layui-input" id="changeDeviceName" name="changeDeviceName" type="text" style="width:128px; margin-left: 27px; margin-top:5px; margin-bottom:15px; display: inline;">'+
                        '<input class="layui-input" id="deviceVersion" name="deviceVersion" type="text" style="width:128px; margin-left: 60px; margin-top:5px; margin-bottom:15px; display: inline;">'+
                        '<input class="layui-input" id="deviceCount" name="deviceCount" type="text" style="width:128px; margin-left: 55px; margin-top:5px; margin-bottom:15px; display: inline;">'+
                        '<input class="layui-input" id="deviceUnit" name="deviceUnit" type="text" style="width:128px; margin-left: 27px; margin-top:5px; margin-bottom:15px; display: inline;"><br>'+
                        '&emsp;&emsp;维修结果&emsp;'+
                        '<textarea class="layui-textarea" id="repairResult" name="repairResult" style="width: 585px; height: 100px; margin-top: 10px; margin-left: 100px; margin-bottom: 15px"></textarea>'+
                        '</div>'+
                        '<button class="layui-btn layui-btn-blue" id="subBtn" lay-submit lay-filter="addform" style="margin-left:250px; margin-top: 25px; margin-bottom: 25px">提交</button>&emsp;&emsp;&emsp;&emsp;'+
                        '<button type="reset" class="layui-btn layui-btn-primary">重置</button>'+
                        '</form>'
                    ,success:function(){
                        //报修时间
                        laydate.render({
                            elem: '#repairTime',
                            type: 'datetime'
                        });
                      	//下拉查找所有设备
                        $.ajax({	
    		  	            type: 'POST',
    		  	            url: '${pageContext.request.contextPath}/device/queryAllDeviceList.do',
    		  	            dataType: 'json',
    		  	            async: false,
    		  	            success: function (data){
    		  	                for (var i = 0; i < data.length; i++) {
    		  	                    $("#deviceNo").append("<option value='"+ data[i].deviceNo +"'>"+ data[i].deviceNo +"</option>");
    		  	                }
    		  	            }
    		  	        });
                        layui.form.render('select');
                    }
                });
            break;
            //批量删除
            case 'delete':
                var data = checkStatus.data;
                var idArr = new Array();
                if(data.length==0){
                    toastr.warning("请至少选择一条记录！");
                }
                for(var i=0;i<data.length;i++){
                    idArr[i] = data[i].id;
                }
                $.ajax({
                    type:'post',
                    url:'${pageContext.request.contextPath }/maintResume/deleteDeviceMaintenanceRecordsById.do',
                    data:{"idArr" : idArr},
                    success:function(data){
                        layer.confirm('确定删除吗？', function(index){
                        	if(data > 0){
                                toastrStyle();
                                toastr.success("删除成功！");
                                setTimeout(function(){
                                    location.reload();
                                },1000);
                            }else{
                                toastrStyle();
                                toastr.warning("删除失败！");
                                setTimeout(function(){
                                    location.reload();
                                },1000);
                            }
                        });
                    }
                });
            break;
	    };
	});

    //监听行工具事件
    table.on('tool(test)', function(obj){
        var data = obj.data;
        var idArr = new Array();
        /*console.log(obj)*/
        //单个删除
        if(obj.event === 'del'){
            layer.confirm('确定删除吗？', function(index){
                var id = obj.data.id;
                idArr[0] = id;
                $.ajax({
                    type:'post',
                    url:'${pageContext.request.contextPath }/maintResume/deleteDeviceMaintenanceRecordsById.do',
                    data:{ "idArr" : idArr },
                    success:function(data){
                        if(data > 0){
                            toastrStyle();
                            toastr.success("删除成功！");
                            setTimeout(function(){
                                location.reload();
                            },1000);
                        }else{
                            toastrStyle();
                            toastr.warning("删除失败！");
                            setTimeout(function(){
                                location.reload();
                            },1000);
                        }
                    }
                })
                layer.close(index);
            });
        }
    });
	/**
     * 表单校验
     */
    /*form.verify({
    	//value：表单的值、item：表单的DOM对象
    	email: function(value, item){
    		if(value == ''){
    			return '不能为空';
    		}
    	}
    });*/
    /**
     * 通用表单提交3(AJAX方式)（设备维修申请表登记）
     */
    form.on('submit(addform)', function (data) {
    	$("#subBtn").attr("disabled",true);
        $.ajax({
            url : '${pageContext.request.contextPath}/maintResume/addDeviceMaintenanceRecords.do',
            data: $("#addformID").serialize(),
            cache : false,
            type : "post",
        }).done(
            function(res) {
                if (res > 0) {
                    toastrStyle();
                    toastr.success('新增成功！');
                    setTimeout(function(){
                        location.reload();

                    },1000);
                }
            }
        ).fail(
            function(res) {
                toastrStyle();
                toastr.error('新增失败！');
                setTimeout(function(){
                    location.reload();
                },1000);
            }
        )
        return false;
    });
    
  	//查找带回
    form.on('select(displayInfo)', function(data){
    	$.ajax({
            type: 'POST',
            url: '${pageContext.request.contextPath}/device/queryDeviceInfoByNo.do',
            data: {deviceNo:$('#deviceNo').val()},
            /* dataType: 'json', */
            /* async: false, */
            success: function (data){
            	var device = eval('(' +data+ ')');
            	$('#deviceName').val(device.deviceName);
            }
        });
    });
});
toastr.options.positionClass = 'toast-top-center'; //提示框的位置设置为中上
//提示框样式
function toastrStyle(){
    toastr.options = {
        "closeButton": false,
        "debug": false,
        "newestOnTop": false,
        "progressBar": false,
        "positionClass": "toast-top-center",
        "preventDuplicates": false,
        "onclick": null,
        "showDuration": "5000",
        "hideDuration": "1000",
        "timeOut&quot": "100000",
        "extendedTimeOut": "0",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    };
}

//根据保养记录表ID查询更换设备详细
function deviceMaintenanceRecordDetailEvent(mId){
	layer.open({
        type: 1 					//Page层类型
        ,area: ['500px', '450px']   //宽  高
        ,title: '更换设备详细'
        ,shade: 0.6 				//遮罩透明度
        ,maxmin: true 			    //允许全屏最小化
        ,anim: 1 					//0-6的动画形式，-1不开启
        /* ,content: $("#lookupTable2") */
        ,content: '<table class="layui-hide" id="lookupTable1" lay-filter="lookupTable1"></table>'
        ,success: function(){
            layui.use(['table'], function(){
                var table = layui.table;
                table.render({
                    elem: '#lookupTable1'
                    ,url:'${pageContext.request.contextPath }/maintResume/queryDeviceMaintenanceRecordDetailById.do?mId='+mId
                    /*,where:{dId:thisRowId.id}*/
                    /*,toolbar: '#toolbar'*/
                    ,title: '更换设备详细'
                    ,id :'contenttable'
                    ,limits:[10,20,30]
                    ,cols: [[
                        /*{type: 'checkbox', fixed: 'left'},*/
                        {field:'id', title:'ID', fixed: 'left', unresize: true, sort: true, type:'numbers'},
                        {field:'changeDeviceName', title:'更换设备名称'},
                        {field:'deviceVersion', title:'型号规格'},
                        {field:'deviceCount', title:'数量'},
                        {field:'deviceUnit', title:'单位'}
                    ]]
                    ,page: false
                });
                //行单击事件
                /* table.on('row(lookupTable2)', function(obj){
                  alert("单击事件")
                }); */
                //行双击事件
                /*table.on('rowDouble(lookupTable1)', function(rowData){
                    console.log(rowData.data.uaddress);
                    layer.closeAll();
                    $("#deviceName").val(rowData.data.uaddress);
                    $("#deviceNo").val(rowData.data.uname);
                });*/
            });
        }
    });
}
</script>
</body>
</html>